import express from 'express';
import { PathMappingService } from '../services/PathMappingService';
import { PathMappingConfigService } from '../services/PathMappingConfigService';

const router = express.Router();

/**
 * 获取当前路径映射配置
 */
router.get('/config', (req, res) => {
  try {
    const dockerInfo = PathMappingService.getDockerInfo();
    const config = PathMappingConfigService.loadConfig();
    
    res.json({
      success: true,
      data: {
        isDocker: dockerInfo.isDocker,
        projectRoot: dockerInfo.projectRoot,
        fallbackHostRoots: dockerInfo.supportedHostRoots,
        customMappings: dockerInfo.customMappings,
        settings: config.settings,
        defaultMappings: config.defaultMappings
      }
    });
  } catch (error) {
    console.error('获取路径映射配置失败:', error);
    res.status(500).json({
      success: false,
      error: '获取路径映射配置失败'
    });
  }
});

/**
 * 添加自定义路径映射
 */
router.post('/custom', (req, res) => {
  try {
    const { hostPath, containerPath, description } = req.body;
    
    if (!hostPath) {
      return res.status(400).json({
        success: false,
        error: '宿主机路径不能为空'
      });
    }
    
    // 验证路径格式
    if (!hostPath.startsWith('/') && !hostPath.match(/^[A-Za-z]:/)) {
      return res.status(400).json({
        success: false,
        error: '宿主机路径格式不正确，应为绝对路径'
      });
    }
    
    const finalContainerPath = containerPath || '/projects';
    const finalDescription = description || `自定义映射: ${hostPath}`;
    
    PathMappingService.addCustomMapping(hostPath, finalContainerPath, finalDescription);
    
    res.json({
      success: true,
      message: '自定义路径映射添加成功',
      data: {
        hostPath,
        containerPath: finalContainerPath,
        description: finalDescription
      }
    });
  } catch (error) {
    console.error('添加自定义路径映射失败:', error);
    res.status(500).json({
      success: false,
      error: '添加自定义路径映射失败'
    });
  }
});

/**
 * 删除自定义路径映射
 */
router.delete('/custom', (req, res) => {
  try {
    const { hostPath } = req.body;
    
    if (!hostPath) {
      return res.status(400).json({
        success: false,
        error: '宿主机路径不能为空'
      });
    }
    
    const removed = PathMappingService.removeCustomMapping(hostPath);
    
    if (removed) {
      res.json({
        success: true,
        message: '自定义路径映射删除成功'
      });
    } else {
      res.status(404).json({
        success: false,
        error: '未找到指定的路径映射'
      });
    }
  } catch (error) {
    console.error('删除自定义路径映射失败:', error);
    res.status(500).json({
      success: false,
      error: '删除自定义路径映射失败'
    });
  }
});

/**
 * 获取所有映射规则
 */
router.get('/mappings', (req, res) => {
  try {
    const mappings = PathMappingService.getAllMappings();
    
    res.json({
      success: true,
      data: mappings
    });
  } catch (error) {
    console.error('获取映射规则失败:', error);
    res.status(500).json({
      success: false,
      error: '获取映射规则失败'
    });
  }
});

/**
 * 更新映射设置
 */
router.put('/settings', (req, res) => {
  try {
    const settings = req.body;
    
    PathMappingService.updateMappingSettings(settings);
    
    res.json({
      success: true,
      message: '映射设置更新成功'
    });
  } catch (error) {
    console.error('更新映射设置失败:', error);
    res.status(500).json({
      success: false,
      error: '更新映射设置失败'
    });
  }
});

/**
 * 测试路径映射
 */
router.post('/test', (req, res) => {
  try {
    const { hostPath } = req.body;
    
    if (!hostPath) {
      return res.status(400).json({
        success: false,
        error: '宿主机路径不能为空'
      });
    }
    
    const mappedPath = PathMappingService.mapHostPathToContainer(hostPath);
    const isValid = PathMappingService.validateMappedPath(mappedPath);
    
    res.json({
      success: true,
      data: {
        hostPath,
        mappedPath,
        isValid,
        isDocker: PathMappingService.isRunningInDocker()
      }
    });
  } catch (error) {
    console.error('测试路径映射失败:', error);
    res.status(500).json({
      success: false,
      error: '测试路径映射失败'
    });
  }
});

/**
 * 获取路径映射建议
 */
router.post('/suggestion', (req, res) => {
  try {
    const { hostPath } = req.body;
    
    if (!hostPath) {
      return res.status(400).json({
        success: false,
        error: '宿主机路径不能为空'
      });
    }
    
    const suggestion = PathMappingService.getPathMappingSuggestion(hostPath);
    
    res.json({
      success: true,
      data: suggestion
    });
  } catch (error) {
    console.error('获取路径映射建议失败:', error);
    res.status(500).json({
      success: false,
      error: '获取路径映射建议失败'
    });
  }
});

export default router;